<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .error{
            color:red;
        }
    </style>
    <script src="src/js/jquery-3.3.1.js"></script>
    <script src="src/js/jquery.validate.js"></script>
    <script src="src/js/additional-methods.js"></script>
</head>
<body>
<form>
    <p>
        <label>
            用户名 <input name="uname" placeholder="用户名">
        </label>
    </p>

    <p>
        <label>
            密码 <input name="upwd" id="upwd" placeholder="密码">
        </label>
    </p>

    <p>
        <label>
            确认密码 <input name="upwd1" placeholder="确认密码">
        </label>
    </p>
    <p>
        <button>注册</button>
    </p>

</form>
</body>
</html>
<script>
    $(function () {
        $.validator.addMethod("checkName",function (val,el,para) {
            let reg=/^[a-z]+\w+$/i;
            return reg.test(val);
        })
        $("form").validate({
            rules:{
                uname:{
                    required:true,
                    checkName:true,
                    rangelength:[6,11],
                    remote:{
                        url:"http://127.0.0.1:80/diwudadao/sever/checkForm.php",
                        type:"get",
                        data:{
                            "uname":function () {
                                return $("input[name=uname]").val();
                            }

                        }
                    }

                    },
                upwd:{
                    required:true,
                    rangelength:[6,8]
                },
                upwd1:{
                    required:true,
                    equalTo:"#upwd"
                }
                },
            messages:{
                uname:{
                    required:"用户名必填",
                    checkName:"用户名不合法",
                    rangelength:"用户名长度为{0}-{1}",
                    remote:"该用户名不存在"
                },
                upwd:{
                    required:"密码必填",
                    rangelength:"密码长度为{0}-{1}"
                },
                upwd1:{
                    required:"请再次输入密码",
                    equalTo:"两次密码不一致"
                }
            },
            submitHandler:function () {
                $.ajax({
                    url:"http://127.0.0.1:80/diwudadao/sever/formRegister.php",
                    type:"post",
                    dataType:"json",
                    // data:{
                    //     unameA:$("input[name=uname]").val(),
                    //     upwd:$("input[name=upwd]").val(),
                    //     usex:$("input[name=usex]").val(),
                    //     utel:$("input[name=utel]").val(),
                    //     uemail:$("input[name=uemail]").val()
                    // }

                    data:$("form").serialize()
                }).then(function(result){
                    if (result.status==1){
                        alert(result.msg);

                    }else{
                        alert(result.msg);

                    }
                })
                alert("注册成功")
                return false;
            }





        })
    })
</script>